package saati.core.statistic.pirson;

import com.numericalmethod.suanshu.stats.distribution.univariate.ChiSquareDistribution;

public class PirsonChecker{
    private static final double probability = 0.05d;

    public static boolean checkTheory(int m, int n, double concordance) {
        double xpSquare = calculateXPSquare(m, n, concordance);
        double degreesOfFreedom = calculateDegreesOfFreedom(n);
        double chiSquare = calculateChiSquare(degreesOfFreedom);
        return (xpSquare >= chiSquare);
    }

    private static double calculateDegreesOfFreedom(int n) {
        return n - 1;
    }

    private static double calculateChiSquare(double degreesOfFreedom) {
        ChiSquareDistribution distribution = new ChiSquareDistribution(degreesOfFreedom);
        return distribution.quantile(probability);
    }

    private static double calculateXPSquare(int m, int n, double concordance) {
        return m * (n - 1) * concordance;
    }
}
